package com.redteam.xomtro.fragment;

import android.app.Activity;
import android.graphics.Typeface;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBar;
import android.view.View;
import android.widget.AbsListView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.redteam.xomtro.MainActivity;
import com.redteam.xomtro.R;
import com.redteam.xomtro.views.EDGirdView;
import com.slidingmenu.lib.SlidingMenu;


public class MainFragment extends Fragment implements EDGirdView.OnDetectScrollListener {
    public final ActionBar actionBar;
    public final View emptyView;
    protected DisplayImageOptions options;
    protected DisplayImageOptions optionsAvatar;
    protected ImageLoader imageLoader = ImageLoader.getInstance();
    protected SlidingMenu slidingMenu;
    public FragmentManager fragmentManager;
    public final Activity activity;
    public static Typeface fontLight;
    public static Typeface fontBold;

    public MainFragment(MainActivity mainActivity) {
        this.slidingMenu = mainActivity.getSlidingMenu();
        this.activity = mainActivity;
        this.actionBar = mainActivity.getSupportActionBar();
        this.fragmentManager = mainActivity.getSupportFragmentManager();
        emptyView = activity.getLayoutInflater().inflate(R.layout.empty_view, null);
        fontLight = Typeface.createFromAsset(activity.getAssets(), "fonts/segoeui_sl.ttf");
        fontBold = Typeface.createFromAsset(activity.getAssets(), "fonts/segoeui_sb.ttf");
        imageLoader.init(ImageLoaderConfiguration.createDefault(activity));
        imageLoader.handleSlowNetwork(true);
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.im_default_small)
                .showImageOnFail(R.drawable.im_default_small)
                .cacheInMemory(true)
                .cacheOnDisc(true)
                .considerExifParams(true)
                .build();
        optionsAvatar = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.im_avatar)
                .showImageOnFail(R.drawable.im_avatar)
                .cacheInMemory(true)
                .cacheOnDisc(true)
                .considerExifParams(true)
                .build();
    }

    protected void beginTransaction(MainFragment fragment) {
        imageLoader.stop();
        fragmentManager.beginTransaction()
                .setCustomAnimations(R.anim.fade_in, R.anim.fade_out)
                .replace(R.id.content_frame, fragment)
                .commit();
    }

    protected void beginTransaction(MainFragment fragment, int inAnimation, int outAnimation) {
        imageLoader.stop();
        fragmentManager.beginTransaction()
                .setCustomAnimations(inAnimation, outAnimation)
                .replace(R.id.content_frame, fragment)
                .commit();
    }

    @Override
    public void onLowMemory() {
        System.gc();
        imageLoader.stop();
        imageLoader.clearDiscCache();
        imageLoader.clearMemoryCache();
    }

//    @Override
//    public void onUpScrolling() {
//        actionBar.show();
//    }
//
//    @Override
//    public void onDownScrolling() {
//        actionBar.hide();
//    }

    @Override
    public void onUpScrolling(AbsListView absListView, int firstVisibleItem) {
          actionBar.show();
    }

    @Override
    public void onDownScrolling(AbsListView absListView, int firstVisibleItem) {
        actionBar.hide();
    }
}
